package com.meihua.baoleyi.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseFragment;
import com.meihua.baoleyi.bean.BannerBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.ui.SelectDiseaseActivity;
import com.meihua.baoleyi.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/16 0024.
 * 儿推服务
 */

public class SonServiceFragment extends BaseFragment implements View.OnClickListener {
    Context mcontext;
    @Bind(R.id.iv_son)
    ImageView mImg;
    private TextView mTv_submit;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sonservice_fragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTv_submit = (TextView) view.findViewById(R.id.submit);
        mTv_submit.setOnClickListener(this);
        geServerData();//请求数据
    }

    //请求数据
    public void geServerData() {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("cate_id", "7");//cate_id	是	int	1代表首页，3代表商城，7代表儿推（1张，非轮播图）
        NetHelper.get(APIServer.BANNERURL, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                BannerBean bean = NetHelper.getBeanForJson(BannerBean.class, s);
                if (bean == null) return;
                if (bean.getCode().equals("100")) {
                    //请求成功
                    try {
                        Glide.with(getActivity()).load(bean.getData().get(0).getSlide_img()).placeholder(R.mipmap.chanpin).into(mImg);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                        Toast.makeText(mcontext,"服务器异常",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit://提交
                getBolcking();//获取儿推访问
                break;
        }
    }

    /**
     * 获取儿推访问
     */
    public void getBolcking() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());//用户id
        NetHelper.post(APIServer.LOGIN_GETBLOCKING, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    JSONObject object = new JSONObject(s);
                    if (object.optString("blocking").equals("400")) {
                        ToastUtil.show(getActivity(), "只有非冻结的会员才可以使用该服务");
                        dismissDialog();
                        return;
                    }
                    startActivity(new Intent(getActivity(), SelectDiseaseActivity.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(mcontext,"服务器异常",Toast.LENGTH_SHORT).show();
                }
                dismissDialog();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
